<?php
/**
 * 首页控制器
 * @author jason <yangjs17@yeah.net> 
 * @version TS3.0
 */
class TestDataAction extends Action {
    /**
     * 架构函数 
     * @access public
     */
    var $userGroup=0;
    var $classId=0;
    var $year=array();
    public function __construct() {
        $user_Group=model("UserGroupLink")->where("uid=".$_SESSION['mid'])->find();
        $this->userGroup=$user_Group["user_group_id"];
        if($user_Group["user_group_id"]==5){
            $Class=M("class")->where("admin_id=".$_SESSION['mid'])->find();
            $this->classId=$Class["class_id"];
            $this->assign('classId', $this->classId); //当前登陆人所属的班级id
        }
        $this->assign('userGroup', $this->userGroup); //当前登陆人的用户组关系
        $this->year=array(
            '0'=>date('Y'),
            '1'=>date('Y',strtotime("-1 year")),
            '2'=>date('Y',strtotime("-2 year")),
        );
    }
	/**
	 * 我的首页
	 *
	 * @return void
	 */
	public function index() {
        $this->assign("year",$this->year);
		$this->display();
	}
	//自测数据
	public function TestData(){
        $this->assign("year",$this->year);
		$this->display();
	}
    //添加上报测试数据
    public function addPeportTest(){
        if($this->userGroup==5){
            $student=D("student")->where("class_id=".$this->classId)->findALL();
            $this->assign("student",$student);
        }
        $test_project=M("test_project")->findALl();
        $this->assign("year",$this->year);
        $this->assign("test_project",$test_project);
        $this->display();
    }
    //添加上报测试数据（操作）
    public function doPeportTest(){
        $class = M('class')->where('class_id='.intval($_POST['class_id']))->find();
        $data=array(
            'stu_id'=>$_POST['stu'],
            'year'=>$_POST['year'],
            'date'=>$_POST['date'],
            'test_number'=>intval($_POST['test_number']),
            'modes_transportation'=>intval($_POST['modes_transportation']),
            'balanceBeam'=>$_POST['balanceBeam'],
            'continuousJump'=>$_POST['continuousJump'],
            'tennis_throw'=>$_POST['tennis_throw'],
            'standing_long_jump'=>$_POST['standing_long_jump'],
            'flexion'=>$_POST['flexion'],
            'reentry_run'=>$_POST['reentry_run'],
            'vision_right'=>$_POST['vision_right'],
            'vision_left'=>$_POST['vision_left'],
            'skinfold_abdomen'=>$_POST['skinfold_abdomen'],
            'skinfold_shoulder_armor'=>$_POST['skinfold_shoulder_armor'],
            'skinfold_upper_arm'=>$_POST['skinfold_upper_arm'],
            'heart_rate'=>$_POST['heart_rate'],
            'sit_height'=>$_POST['sit_height'],
            'weight'=>$_POST['weight'],
            'height'=>$_POST['height'],
            'chest'=>$_POST['chest'],
            'is_report'=>1,
            'class_id'=>intval($_POST['class_id']),
            'class_type' => $class['class_type'],
            'admin_id'=>$this->mid,
            'ctime'=>time(),
        );
        $where=array(
            'is_report'=>1,
            'year'=>$_POST['year'],
            'stu_id'=>intval($_POST['stu']),
            'test_number'=>intval($_POST['test_number']),
            'class_type' => $class['class_type'],
            'is_del' =>0,
        );
        if(D("TestData")->where($where)->find()){
            echo "本次测试已经有数据";exit();
        }else{
            $res=D("TestData")->add($data);
            if($res){
                $data['class_type'] = $class['class_type'];
                D("HealthReport")->addHeathReport($data,$res);
                echo $res;exit();
            }else{
                echo "添加失败，请重试";exit();
            }
        }
    }
	//添加测试
	public function addTest(){
        if($this->userGroup==5){
            $student=D("student")->where("class_id=".$this->classId)->findALL();
            $this->assign("student",$student);
        }
		$test_project=M("test_project")->findALl();
		$this->assign("year",$this->year);
		$this->assign("test_project",$test_project);
		$this->display();
	}
	//添加测试（操作）
	public function doAddTest(){
        $class = M('class')->where('class_id='.intval($_POST['class_id']))->find();
		$data=array(
			'stu_id'=>intval($_POST['stu']),
			'year'=>$_POST['year'],
			'date'=>$_POST['date'],
			'test_number'=>intval($_POST['test_number']),
			'modes_transportation'=>intval($_POST['modes_transportation']),
			'balanceBeam'=>$_POST['balanceBeam'],
			'continuousJump'=>$_POST['continuousJump'],
			'tennis_throw'=>$_POST['tennis_throw'],
			'standing_long_jump'=>$_POST['standing_long_jump'],
			'flexion'=>$_POST['flexion'],
			'reentry_run'=>$_POST['reentry_run'],
			'vision_right'=>$_POST['vision_right'],
			'vision_left'=>$_POST['vision_left'],
			'skinfold_abdomen'=>$_POST['skinfold_abdomen'],
			'skinfold_shoulder_armor'=>$_POST['skinfold_shoulder_armor'],
			'skinfold_upper_arm'=>$_POST['skinfold_upper_arm'],
			'heart_rate'=>$_POST['heart_rate'],
			'sit_height'=>$_POST['sit_height'],
			'weight'=>$_POST['weight'],
			'height'=>$_POST['height'],
			'chest'=>$_POST['chest'],
            'class_id'=>intval($_POST['class_id']),
            'class_type' => $class['class_type'],
            'is_report'=>0,
			'admin_id'=>$this->mid,
			'ctime'=>time(),
		);
		$where=array(
            'is_report'=>0,
			'year'=>$_POST['year'],
			'stu_id'=>intval($_POST['stu']),
			'test_number'=>intval($_POST['test_number']),
            'class_type' => $class['class_type'],
            'is_del' =>0,
		);
		if(D("test_data")->where($where)->find()){
			echo "本次测试已有数据";exit();
		}else{
			$res=D("test_data")->add($data);
			if($res){
                $data['class_type'] = $class['class_type'];
                D("HealthReport")->addHeathReport($data,$res);
				echo $res;exit();
			}else{
				echo "添加失败，请重试";exit();
			}
		}
	}
	//修改测试数据
	function updatePeportTest(){
		$test_id=intval($_GET["id"]);
		$data=D("TestData")->where("is_del=0 and test_id=".$test_id)->find();
        $class_info = M('class')->where('class_id='.$data['class_id'])->find();

        

        $class_id = $data['class_id'];
        if($data["class_type"]==1){
            $data['class_type'] = "大班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' => array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==2){
            $data['class_type'] = "中班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==3){
            $data['class_type'] = "小班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else{
            $data['class_type'] = "混龄班";
            $data['class_name'] = $class_info['class_name'];
        }
        
		$data["stu"]=D("Student")->getstudentById($data["stu_id"]);
		$test_project=M("test_project")->findALl();
		$this->assign("test_project",$test_project);
		$this->assign("data",$data);
		$this->display();
	}
    //修改测试数据
    function UpdateTest(){
        $test_id=intval($_GET["id"]);
        $data=D("TestData")->where("is_del=0 and test_id=".$test_id)->find();
        $class_info = M('class')->where('class_id='.$data['class_id'])->find();

        

        $class_id = $data['class_id'];
        if($data["class_type"]==1){
            $data['class_type'] = "大班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' => array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==2){
            $data['class_type'] = "中班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==3){
            $data['class_type'] = "小班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else{
            $data['class_type'] = "混龄班";
            $data['class_name'] = $class_info['class_name'];
        }

        $data["stu"]=D("Student")->getstudentById($data["stu_id"]);
        $test_project=M("test_project")->findALl();
        $this->assign("test_project",$test_project);
        $this->assign("data",$data);
        $this->display();
    }
	//测试数据详细
	function TestDataview(){
		$test_id=intval($_GET["id"]);
        $type=intval($_GET["type"]);
		$data=D("TestData")->where("is_del=0 and test_id=".$test_id)->find();
		$data["stu"]=D("Student")->getstudentById($data["stu_id"]);

        $class_info = M('class')->where('class_id='.$data['class_id'])->find();

        

        $class_id = $data['class_id'];
        if($data["class_type"]==1){
            $data['class_type'] = "大班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' => array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%大%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==2){
            $data['class_type'] = "中班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%中%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else if($data["class_type"]==3){
            $data['class_type'] = "小班";
            if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name1'];
            }
            if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%小%班%') ) )->find() ) {
                $data['class_name'] = $class_info['class_name2'];
            }
        }else{
            $data['class_type'] = "混龄班";
            $data['class_name'] = $class_info['class_name'];
        }

            
		$test_project=M("test_project")->findALl();
		$this->assign("test_project",$test_project);
        $this->assign("type",$type);
		$this->assign("data",$data);
		$this->display();
	}
	//修改测试（操作）
	public function doSaveTest(){
		$test_id=intval($_POST["test_id"]);
        $test_info = M("test_data")->where("is_del=0 and test_id=".$test_id)->find();
		$data=array(
			'balanceBeam'=>$_POST['balanceBeam'],
			'continuousJump'=>$_POST['continuousJump'],
			'tennis_throw'=>$_POST['tennis_throw'],
			'standing_long_jump'=>$_POST['standing_long_jump'],
			'flexion'=>$_POST['flexion'],
			'reentry_run'=>$_POST['reentry_run'],
			'vision_right'=>$_POST['vision_right'],
			'vision_left'=>$_POST['vision_left'],
			'skinfold_abdomen'=>$_POST['skinfold_abdomen'],
			'skinfold_shoulder_armor'=>$_POST['skinfold_shoulder_armor'],
			'skinfold_upper_arm'=>$_POST['skinfold_upper_arm'],
			'heart_rate'=>$_POST['heart_rate'],
			'sit_height'=>$_POST['sit_height'],
			'weight'=>$_POST['weight'],
			'height'=>$_POST['height'],
			'chest'=>$_POST['chest'],
            'is_update'=>1,
            'year'=>$test_info['year'],
            'stu_id'=>$test_info['stu_id'],
            'test_number'=>$test_info['test_number'],
            'is_report'=>$test_info['is_report'],
			'class_type'=>$test_info['class_type'],
		);
		$res=D("test_data")->where("test_id=".$test_id)->save($data);
        
		if($res !== false ){
            D("HealthReport")->addHeathReport($data,$test_id);
            echo $res;exit();
		}else{
			echo "操作失败，请重试";exit();
		}
	}
	//删除测试数据
	function DeleteTest(){
		$data["is_del"]=1;
		$res=D("TestData")->where("test_id=".intval($_POST["id"]))->save($data);
		if($res){
			echo $res;exit;
		}else{
			echo "删除失败，请重试";exit;
		}
	}
	//数据导出
	function doexport(){
        $type=intval($_GET["type"]);
        $year=$_GET["year"];
        $test_number=intval($_GET["test_number"]);
        $Class=intval($_GET["Class"]);
		$test_id=intval($_GET["id"]);
		$where["is_del"]=0;
        if($Class){
            $where['class_id']=$Class;
        }
        if($year){
            $where['year']=$year;
        }
        if($type){
            $where['is_report']= $type==1 ? 1 : 0;
        }
        if($test_number){
            $where['test_number']=$test_number;
        }
        if($test_id){
            $where["test_id"]=$test_id;
        }
		$data=D("TestData")->where($where)->findALL();
		if(empty($data)){
            $this->error("没有测试相关的数据！");die;
        }
		foreach ($data as $key => $value) {
			$data[$key]['stu']=D("student")->getstudentById($value["stu_id"]);
		}
        include 'PHPExcel/PHPExcel.php';
        include 'PHPExcel/PHPExcel/Writer/Excel5.php';
        include 'PHPExcel/PHPExcel/Writer/Excel2007.php';
        $objPHPExcel = new PHPExcel();
        /* 设置输出的excel文件为2007兼容格式 */
        //$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $objWriter=new PHPExcel_Writer_Excel5($objPHPExcel);
        /* 设置当前的sheet */
        $objPHPExcel->setActiveSheetIndex(0);
        $objActSheet = $objPHPExcel->getActiveSheet();
        /* sheet标题 */
        $objActSheet->setTitle("测试数据导出");
        //合并单元格
        $objActSheet->mergeCells('A1:U1');
        $objActSheet->setCellValue('A1',"测试数据导出");
        $objStyleA1 =$objPHPExcel->getActiveSheet()->getStyle('A1');
        $objActSheet->setCellValue("A2","学生名称");
        $objActSheet->setCellValue("B2","所属班级");
        $objActSheet->setCellValue("C2","测试时间");
        if($type==1){
            $objActSheet->setCellValue("D2","测试季度");
        }else{
            $objActSheet->setCellValue("D2","测试次数");
        }
        $objActSheet->setCellValue("E2","入园方式");
        $objActSheet->setCellValue("F2","走平衡木");
        $objActSheet->setCellValue("G2","双脚连续跳");
        $objActSheet->setCellValue("H2","网球投掷");
        $objActSheet->setCellValue("I2","立定跳远");
        $objActSheet->setCellValue("J2","坐位体前屈");
        $objActSheet->setCellValue("K2","十米折返跑");
        $objActSheet->setCellValue("L2","右眼视力");
        $objActSheet->setCellValue("M2","左眼视力");
        $objActSheet->setCellValue("N2","腹部皮褶");
        $objActSheet->setCellValue("O2","肩胛皮褶");
        $objActSheet->setCellValue("P2","上臂皮褶");
        $objActSheet->setCellValue("Q2","心率");
		$objActSheet->setCellValue("R2","坐高");
        $objActSheet->setCellValue("S2","体重");
        $objActSheet->setCellValue("T2","身高");
        $objActSheet->setCellValue("U2","胸围");
        $objStyleA1->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//设置垂直居中
        $objStyleA1->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置横向居中
        //颜色填充
        $objStyleA1->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        $objStyleA1->getFill()->getStartColor()->setARGB(PHPExcel_Style_Color::COLOR_YELLOW);
        //字体设置
        $objStyleA1->getFont()->setName('Candara');
        $objStyleA1->getFont()->setSize(16);
        $objStyleA1->getFont()->setBold(true);
        $objStyleA1->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
        $objStyleA1->getFont()->setBold(true);                      
        $objActSheet->getColumnDimension('A')->setWidth(15);
        $objActSheet->getColumnDimension('B')->setWidth(15);
        $objActSheet->getColumnDimension('C')->setWidth(15);
        $objActSheet->getColumnDimension('D')->setWidth(15);
        $objActSheet->getColumnDimension('E')->setWidth(15);
        $objActSheet->getColumnDimension('F')->setWidth(15);
        $objActSheet->getColumnDimension('H')->setWidth(15);
        $objActSheet->getColumnDimension('I')->setWidth(15);
        $objActSheet->getColumnDimension('J')->setWidth(15);
        $objActSheet->getColumnDimension('K')->setWidth(15);
        $objActSheet->getColumnDimension('L')->setWidth(15);
        $objActSheet->getColumnDimension('M')->setWidth(15);
        $objActSheet->getColumnDimension('N')->setWidth(15);
        $objActSheet->getColumnDimension('O')->setWidth(15);
        $objActSheet->getColumnDimension('P')->setWidth(15);
        $objActSheet->getColumnDimension('Q')->setWidth(15);
        $objActSheet->getColumnDimension('R')->setWidth(15);
        $objActSheet->getColumnDimension('S')->setWidth(15);
        $objActSheet->getColumnDimension('T')->setWidth(15);
        $objActSheet->getColumnDimension('U')->setWidth(15);
        for ($i=0; $i <count($data); $i++) {
            $k=$i+3; 
            $stu_name=$data[$i]['stu']['stu_name'];
            $class_type=$data[$i]['stu']['class_type']."/".$data[$i]['stu']['class_name'];
            $idcard=$data[$i]['idcard'];
            $date=$data[$i]['date'];
            if($type==1){
                $test_number= $data[$i]['test_number']==1 ? "春季" : "秋季";
            }else{
              $test_number="第".$data[$i]['test_number']."次";  
            }
            $modes_transportation=$data[$i]['modes_transportation']==1 ? "走路" : "坐车";
            $balanceBeam=$data[$i]['balanceBeam']."秒";
            $continuousJump=$data[$i]['continuousJump']."秒";
            $tennis_throw=$data[$i]['tennis_throw']."米";
            $standing_long_jump=$data[$i]['standing_long_jump']."厘米";
            $flexion=$data[$i]['flexion']."厘米";
            $reentry_run=$data[$i]['reentry_run']."秒";
            $vision_right=$data[$i]['vision_right'];
            $vision_left=$data[$i]['vision_left'];
            $skinfold_abdomen=$data[$i]['skinfold_abdomen']."毫米";
            $skinfold_shoulder_armor=$data[$i]['skinfold_shoulder_armor']."毫米";
            $skinfold_upper_arm=$data[$i]['skinfold_upper_arm']."毫米";
            $heart_rate=$data[$i]['heart_rate']."次/分钟";
            $sit_height=$data[$i]['sit_height']."厘米";
            $weight=$data[$i]['weight']."千克";
            $height=$data[$i]['height']."厘米";
            $chest=$data[$i]['chest']."厘米";
            //设置值
            $objActSheet->setCellValue("A$k","$stu_name");
            $objActSheet->setCellValue("B$k","$class_type");
            $objActSheet->setCellValue("C$k","$date");
            $objActSheet->setCellValue("D$k","$test_number");
            $objActSheet->setCellValue("E$k","$modes_transportation");
            $objActSheet->setCellValue("F$k","$balanceBeam");
            $objActSheet->setCellValue("G$k","$continuousJump");
            $objActSheet->setCellValue("H$k","$tennis_throw");
            $objActSheet->setCellValue("I$k","$standing_long_jump");
            $objActSheet->setCellValue("J$k","$flexion");
            $objActSheet->setCellValue("K$k","$reentry_run");
            $objActSheet->setCellValue("L$k","$vision_right");
            $objActSheet->setCellValue("M$k","$vision_left");
            $objActSheet->setCellValue("N$k","$skinfold_abdomen");
            $objActSheet->setCellValue("O$k","$skinfold_shoulder_armor");
            $objActSheet->setCellValue("P$k","$skinfold_upper_arm");
            $objActSheet->setCellValue("Q$k","$heart_rate");
            $objActSheet->setCellValue("R$k","$sit_height");
            $objActSheet->setCellValue("S$k","$weight");
            $objActSheet->setCellValue("T$k","$height");
            $objActSheet->setCellValue("U$k","$chest");
        }
        /* 生成到浏览器，提供下载 */
        ob_end_clean();  //清空缓存
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
       // header("Content-Type:application/vnd.ms-execl;charset=utf8");
        header("Content-type:application/vnd.ms-excel;charset=gb2312");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=学生测试数据导出.xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
	}

	//提测数据导入
    public function import()
    {
        $file = $_FILES['file'];
        if ($file['error'] == 4) $this->error('请选择上传excel文件');
        $file_types = explode ( ".", $file['name'] );
        $excel_type = array('xls','csv','xlsx');
        if (!in_array(strtolower(end($file_types)),$excel_type)){
            $this->error("不是Excel文件，请重新上传");die;
        }
        include 'PHPExcel/PHPExcel.php';
        include 'PHPExcel/PHPExcel/Writer/Excel5.php';
        include 'PHPExcel/PHPExcel/Writer/Excel2007.php';
        //设置获取excel对象
        $objReader = \PHPExcel_IOFactory::createReader('Excel5');//配置成2003版本，因为office版本可以向下兼容
        $objPHPExcel = $objReader->load($file['tmp_name'],$encode='utf-8');//$file 为解读的excel文件

        $sheet = $objPHPExcel->getSheet(0);
        $excel_array = $objPHPExcel->getsheet(0)->toArray(); //转换为数组格式
        $addData = [];
        foreach ($excel_array as $key=>$val){
            if($key > 0){
                $data = array();
                $stu_name = trim($val[0]);//学生名称
                $class_type_name = trim($val[1]);//年级
                $class_name = trim($val[2]);//班级名称
                $student = M("student")->where("stu_name='$stu_name'")->find();
                if(!$student){
                    $this->error("学生名字不存在");die;
                }
                $data['stu_id'] = $student['stu_id'];
                $data['class_id'] = $student['class_id'];
                $classTypeArr = array('大班'=>'1','中班'=>2,'小班'=>'3','混龄班'=>4);
                $data['class_type'] = $classTypeArr[$class_type_name];
                if(!$data['class_type']){
                    $this->error("年级格式不对");die;
                }
                $data['date'] = trim($val[3]);//提测时间
                $quarterArr = array('春季'=>1,'秋季'=>'2');
                $quarter = trim($val[4]);//测试季度（春季，秋季）
                $data['test_number'] = $quarterArr[$quarter];
                if(!$data['test_number']){
                    $this->error("季度格式不对");die;
                }
                $wayArr = array('走路'=>'1','坐车'=>'2');
                $way = trim($val[5]);//入园方式（1代表走路，2代表坐车）
                $data['modes_transportation'] = $wayArr[$way];
                if(!$data['modes_transportation']){
                    $this->error("入园方式不对");die;
                }
                $data['balanceBeam'] = trim($val[6]);//走平衡木
                $data['continuousJump'] = trim($val[7]);//双脚连续跳
                $data['tennis_throw'] = trim($val[8]);//网球投掷
                $data['standing_long_jump'] = trim($val[9]);//立定跳远
                $data['flexion'] = trim($val[10]);//坐位体前屈
                $data['reentry_run'] = trim($val[11]);//十米折返跑
                $data['vision_right'] = trim($val[12]);//右眼视力
                $data['vision_left'] = trim($val[13]);//右眼视力
                $data['skinfold_abdomen'] = trim($val[14]);//腹部皮褶
                $data['skinfold_shoulder_armor'] = trim($val[15]);//肩胛皮褶
                $data['skinfold_upper_arm'] = trim($val[16]);//上臂皮褶
                $data['heart_rate'] = trim($val[17]);//心率
                $data['sit_height'] = trim($val[18]);//坐高
                $data['weight'] = trim($val[19]);//体重
                $data['height'] = trim($val[20]);//身高
                $data['chest'] = trim($val[21]);//胸围
                $data['ctime'] = time();
                $data['admin_id'] = $_SESSION['mid'];
                $addData[] = $data;
            }
        }
        $this->success('导入成功:.');die;
	}
	/**
     * 取得测试学生列表
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getList() {
        $where=array(
            'is_del'=>0,
            'is_report'=>intval($_GET["is_report"])
        );
       	$name=t($_GET["name"]);
        $year=$_GET["year"];
        $test_number=intval($_GET["test_number"]);
        $Class=intval($_GET["Class"]);
        if($name) {
            //判断当前账号是园级还是班级账号
            $user_group_id = M('user_group_link')->where('uid='.$_SESSION['mid'])->find();
            if($user_group_id['user_group_id'] == 4) {//园级
                $school_id = M("school")->where("fid=".$_SESSION['mid'])->find();
                //获取此学校所有学生
                $stu_id1  = M("student")->where('school_id=' . $school_id['id'])->field('stu_id')->select();
                $stu_id1 = getSubByKey($stu_id1 , "stu_id") ;

                $map["stu_name"] = array("like","%".$name."%");
                $stu_id2 = D("student")->where($map)->field("stu_id")->select();
                $stu_id2 = getSubByKey($stu_id2 , 'stu_id');
                
                $stu_id = array_intersect($stu_id1 , $stu_id2);
                $where['stu_id'] = array('in' ,  $stu_id);
            } elseif($user_group_id['user_group_id'] == 5) {//班级
                //获取此班级所有学生
                $stu_id1  = M("student")->where('class_id=' . $_GET['Class'])->field('stu_id')->select();
                $stu_id1  = getSubByKey($stu_id1 , "stu_id") ;
                
                $map["stu_name"] = array("like","%".$name."%");
                $stu_id2 = D("student")->where($map)->field("stu_id")->select();
                $stu_id2 = getSubByKey($stu_id2 , 'stu_id');
                
                $stu_id = array_intersect($stu_id1 , $stu_id2);
                $where['stu_id'] = array('in' ,  $stu_id);
            }
	    }
        if($year){
            $where['year']=$year;
        }
        if($Class){
            $where['class_id']=$Class;
        }
        if($test_number){
            $where['test_number']=$test_number;
        }
        if($_GET["class_type"]) {
            $where['class_type'] = intval($_GET["class_type"]);
        } else {
            if( $user_group_id['user_group_id'] == 5 ) {
                $where['class_type'] = M('class')->where('`admin_id`='.$_SESSION['mid'])->getField('class_type');
            }
        }
        
        $data = D("test_data")->where($where)->order("stu_id")->findPage(20);

        foreach ($data['data'] as $key => &$value) {
            $value["result"]=D("TestData")->getResutlByTestId($value['test_id']);
            $value["stu"]=D("Student")->getstudentById($value['stu_id']);

            if($Class) {
                $class_info = M('class')->where('class_id='.$Class)->find();
                $class_id   = $Class;
            } else {
                $class_info = M('class')->where('class_id='.$value['class_id'])->find();
                $class_id   = $value['class_id'];
            }

            if($value["class_type"]==1){
                $value['class_type'] = "大班";
                if( M('class')->where( array('class_id'=>$class_id,'class_name' => array('like' ,'%大%班%') ) )->find() ) {
                    $value['class_name'] = $class_info['class_name'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%大%班%') ) )->find() ) {
                    $value['class_name'] = $class_info['class_name1'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%大%班%') ) )->find() ) {
                    $value['class_name'] = $class_info['class_name2'];
                }
            }else if($value["class_type"]==2){
                $value['class_type'] = "中班";
                if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%中%班%') ) )->find() ) {
                    $value['class_name'] = $class_info['class_name'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%中%班%') ) )->find() ) {
                    $value['class_name'] = $class_info['class_name1'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%中%班%') ) )->find() ) {
                    $value['class_name'] = $class_info['class_name2'];
                }
            }else if($value["class_type"]==3){
                $value['class_type'] = "小班";
                if( M('class')->where( array('class_id'=>$class_id,'class_name' =>  array('like' ,'%小%班%') ) )->find() ) {
                    $value['class_name'] = $class_info['class_name'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name1' =>  array('like' ,'%小%班%') ) )->find() ) {
                    $value['class_name'] = $class_info['class_name1'];
                }
                if( M('class')->where( array('class_id'=>$class_id,'class_name2' =>  array('like' ,'%小%班%') ) )->find() ) {
                    $value['class_name'] = $class_info['class_name2'];
                }
            }else{
                $value['class_type'] = "混龄班";
                $value['class_name'] = $class_info['class_name'];
            }
        }



        if($_GET["is_report"]==1){
            $view_list="Report_list";
        }else{
            $view_list="TestData_list";
        }
        
		
        
        if ($data['data']) {
            $this->assign('listData', $data['data']);
            $this->assign('name',$name);
            $this->assign('year',$year);
            $this->assign('Class',$Class);
            $this->assign('year',$year);
            $this->assign('test_number',$test_number);

            
            $html = $this->fetch($view_list);
        } else {
            $html = $this->fetch($view_list);
        }
        $data['data'] = $html;
        
        echo json_encode($data);
        exit();
    }
	/**
     * 取得班级列表
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getClass() {
        $where["is_del"]=0;
        $school=M("school")->where("fid=".$_SESSION['mid'])->find();
        $where["school_id"]=$school["id"];
        $class_type=$_GET["class_type"];
        $where["class_type"] = $class_type;


        $data = M("class")->where($where)->findAll();
        foreach ($data as &$val) {
            if($val['is_up'] == 1) {
                $val['class_name'] = $val['class_name1'];
            } elseif($val['is_up'] == 2) {
                $val['class_name'] = $val['class_name2'];
            }  
        }

        if( $_GET['class_mod'] == 1) {
            $datas = $data;
        } else {
			unset($where["class_type"]);
            $class_type1 = $class_type - 1;
            $class_type2 = $class_type - 2;
            $where["class_type"]  = array('in',"{$class_type1},{$class_type2}");
            $where["is_up"]       = array('neq' , 0);
            $data1 = M("class")->where($where)->findAll();
            
			foreach($data1 as &$val){
				if($val['is_up'] == 1 && ($class_type == 3 || $class_type == 2 ) ) {
					$val['class_name'] = $val['class_name'];
				} 	

                if($val['is_up'] == 2 && $class_type == 3 ) {
                    $val['class_name'] = $val['class_name'];
                }

                if($val['is_up'] == 2 && $class_type == 2 ) {
                    $val['class_name'] = $val['class_name1'];
                }
			}
            if($data && $data1) {
                $datas = array_merge($data , $data1);
            }
            if($data && !$data1) {
                $datas = $data;
            }
            if(!$data && $data1) {
                $datas = $data1;
            }
            if(!$data && !$data1) {
                $datas = array();
            }
        }
        

        if( isset($_GET['is_report']) && $_GET['test_number'] ) {
            if( $_GET['is_report'] == 1) {
                $map['is_report']   = 1;
            } else {
                $map['is_report']   = 0;
            }
            $map['test_number'] = $_GET['test_number'];
            $map['class_type']  = $class_type;
            $map['year']        = $_GET['year'];
            $map['is_del']      = 0;
            $class_ids = M('test_data')->where($map)->field('class_id')->select();
           $class_ids = getSubByKey($class_ids , 'class_id');
            $class_ids = array_unique($class_ids);
            foreach ($datas as $key=>$value) {
                if( !in_array($value['class_id'], $class_ids)) {
                    unset($datas[$key]);
                }
            }
        }
        exit( json_encode($datas) );
    }
    /**
     * 取得学生列表
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getStu() {
        $where["is_del"]=0;
        $class_id=$_GET["Class"];
        if($class_id){
            $where["class_id"]=$class_id;
        }
        $data = M("student")->where($where)->field("stu_id,stu_name")->findAll();
        echo json_encode($data);
        exit();
    }
    /**
     * 取得学生出生状况
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getBirthCondition() {
        $stu=$_GET["stu"];
        if($stu){
            $where["stu_id"]=$stu;
        }
        $data = M("student")->where($where)->field("birth_condition")->findAll();
        echo json_encode($data);
        exit();
    }
    /**
     * 取得地区列表
     * @param boolean $return 是否返回数据，如果不是返回，则会直接输出Ajax JSON数据
     * @return void|array
     */
    public function getArea() {
        $pid=$_GET["pid"];
        $where["pid"]=$pid;
        $data = M("area")->where($where)->findAll();
        echo json_encode($data);
        exit();
    }
}	           
